import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_intrinsic_circle_line.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/core/utils/file_util.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/basic_information/compliance_file_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/basic_information/file_management/file_list/compliance_file_details/compliance_file_details_controller.dart';

/// 合规性文件-文件详情
class ComplianceFileDetailsPage extends StatelessWidget {
  ComplianceFileDetailsPage({super.key});

  final controller = Get.put(ComplianceFileDetailsController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
              appBar: CustomAppBar(
                titleText: '文件详情',
              ),
              body: Container(
                alignment: Alignment.topCenter,
                color: const Color(0xffF5F5F6),
                child: SingleChildScrollView(
                  child: Column(
                    children: [
                      Container(
                        height: 38.w,
                        alignment: Alignment.centerLeft,
                        padding: EdgeInsets.only(left: 16.w),
                        child: Text('基本信息', style: TextStyle(fontSize: 12.sp, color: const Color(0xFF7C7D93))),
                      ),
                      Container(
                        height: 2.w,
                        color: const Color(0xFF587DF5),
                      ),
                      Container(
                        color: Colors.white,
                        padding: EdgeInsets.symmetric(horizontal: 20.w, vertical: 15.w),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(controller.fileEntity.fileName!,
                                style: TextStyle(
                                  fontSize: 16.sp,
                                  fontWeight: FontWeight.w500,
                                  color: const Color(0xFF262F38),
                                )),
                            AppGaps.vGap10,
                            _getTextView(title: '状态：', value: controller.item.statusName!),
                            AppGaps.vGap10,
                            // 文件
                            _getAttachmentView(controller.fileEntity.fileUrl!),
                            AppGaps.vGap10,
                            _getTextView(title: '编制时间：', value: controller.fileEntity.organizationDate!),
                            AppGaps.vGap10,
                            _getTextView(title: '有效期：', value: controller.fileEntity.effectiveDate!),
                            AppGaps.vGap10,
                            Text(
                              '备注:',
                              style: TextStyle(
                                fontSize: 13.sp,
                                height: 1.2,
                                fontWeight: FontWeight.w400,
                                color: const Color(0xffA5A6B5),
                              ),
                            ),
                            AppGaps.vGap8,
                            Container(
                              alignment: Alignment.topLeft,
                              padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 10.w),
                              decoration: BoxDecoration(
                                  color: const Color(0xffF7F7FB),
                                  border: Border.all(color: const Color(0xffEBEDF0), width: 1.w),
                                  borderRadius: BorderRadius.all(Radius.circular(4.r))),
                              child: Text(
                                controller.fileEntity.remark!,
                                style: TextStyle(
                                  fontSize: 14.sp,
                                  height: 1.2,
                                  fontWeight: FontWeight.w400,
                                  color: const Color(0xff495061),
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                      Container(
                        height: 38.w,
                        alignment: Alignment.centerLeft,
                        padding: EdgeInsets.only(left: 16.w),
                        child: Text('更新历史', style: TextStyle(fontSize: 12.sp, color: const Color(0xFF7C7D93))),
                      ),
                      Container(
                        height: 2.w,
                        color: const Color(0xFF587DF5),
                      ),
                      Container(
                        color: Colors.white,
                        child: ListView.builder(
                            padding: EdgeInsets.symmetric(horizontal: 27.w, vertical: 22.w),
                            itemCount: controller.fileEntity.fileHistoryArray!.length,
                            shrinkWrap: true,
                            physics: const BouncingScrollPhysics(),
                            itemBuilder: (context, index) {
                              return CustomIntrinsicCircleLine(
                                showWidget: _getTrainView(controller.fileEntity.fileHistoryArray![index]),
                              );
                            }),
                      ),
                    ],
                  ),
                ),
              ));
        });
  }

  /// 获取附件列显示
  Widget _getAttachmentView(String url) {
    if (url.isEmpty) {
      return Container();
    }
    //本地文件名
    final String currUrlName = url.toString().substring(25);
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        SizedBox(
          width: 70.w,
          child: Text(
            '文件：',
            style: TextStyle(
              fontSize: 13.sp,
              height: 1.2,
              fontWeight: FontWeight.w400,
              color: const Color(0xffA5A6B5),
            ),
          ),
        ),
        Expanded(
            child: Text(
          currUrlName,
          style: TextStyle(
            fontSize: 14.sp,
            height: 1.2,
            color: const Color(0xFF5C6DFF),
          ),
        ).clickOnce(() {
          //打开文件
          FileUtils.openFile(Interface.fileUrl + url);
        })),
      ],
    );
  }

  /// 获取字段显示
  Widget _getTextView({required String title, required String value, Color? valueColor}) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        SizedBox(
          width: 70.w,
          child: Text(
            title,
            style: TextStyle(
              fontSize: 13.sp,
              height: 1.2,
              fontWeight: FontWeight.w400,
              color: const Color(0xffA5A6B5),
            ),
          ),
        ),
        Expanded(
            child: Text(
          value,
          style: TextStyle(
            fontSize: 14.sp,
            height: 1.2,
            fontWeight: FontWeight.w400,
            color: valueColor ?? const Color(0xff495061),
          ),
        ))
      ],
    );
  }

  ///获取历史显示
  Widget _getTrainView(ComplianceFileEntity value) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          children: [
            Expanded(
              child: Text(
                value.updateDate!,
                style: TextStyle(
                  fontSize: 15.sp,
                  fontWeight: FontWeight.w400,
                  color: const Color(0xff262F38),
                ),
              ),
            ),
            Container(
              height: 16.w,
              width: 16.w,
              decoration: BoxDecoration(
                color: const Color(0xff5C6DFF),
                border: Border.all(
                  color: const Color(0xffEEF0FF),
                  width: 4.w,
                ),
                borderRadius: BorderRadius.circular(90.r),
              ),
            ),
            AppGaps.hGap8,
            Text(
              value.updateBy!,
              style: TextStyle(
                fontSize: 14.sp,
                fontWeight: FontWeight.w400,
                color: const Color(0xff5C6DFF),
              ),
            )
          ],
        ),
        AppGaps.vGap6,
        Text(value.fileName!,
            style: TextStyle(
              fontSize: 14.sp,
              fontWeight: FontWeight.w400,
              color: const Color(0xFF262F38),
            )),
        AppGaps.vGap10,
        // 文件
        _getAttachmentView(controller.fileEntity.fileUrl!),
        AppGaps.vGap10,
        _getTextView(title: '编制时间：', value: value.organizationDate!),
        AppGaps.vGap10,
        _getTextView(title: '有效期：', value: value.effectiveDate!),
        AppGaps.vGap10,
        _getTextView(title: '备注：', value: value.remark!),
        AppGaps.vGap20,
      ],
    );
  }
}
